<style>
.float_info{
	box-shadow: 0px 1px 1px 1px rgba(0, 0, 0, 0.1);
	color: #666666;
	transition: all 0.3s;
	position: fixed;
	top: 80px;
	left: 12px;
	width: 100px;
	height: 100px;
	display: flex;
	flex-flow: column;
	justify-content: center;
	align-items: center;
	z-index: 999;
	border-radius: 35px;
	cursor: pointer;
}
.imgIcon{
	width: 100%;
	height: 100%;
}
</style>
<template>
	<!-- @touchstart="touchStart"
	@touchmove="touchmove"
	@touchend="touchend" -->
    <view
        class="float_info"
		@click="onBtnClicked"
    >
    	<image class="imgIcon" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-767674fd-0f0e-4801-90e6-bef27c2e36df/5db98d57-4293-4052-9294-ebdf610b8c87.png">
			<!-- :style="{'width': itemWidth + 'px', 'height': itemHeight + 'px'}" -->
    </view>
</template>
<script>
export default {
    data() {
        return {
            clientWidth: 0,
            clientHeight: 0,
            timer: null,
            currentTop: 0,
            left: 0,
            top: 0,
			floatButton:''
        }
    },
    props: {
        itemWidth: {            // 悬浮按钮宽度
            type: Number,
            default: 80
        },
        itemHeight: {           // 悬浮按钮高度
            type: Number,
            default: 80
        },
        gapWidth: {             // 距离左右两边距离     
            type: Number,
            default: 10
        },
        coefficientHeight: {    // 从上到下距离比例
            type: Number,
            default: 0.55
        }
    },
    created() {
		// info.boundingClientRect(function(data) {
		// 	//	data - 包含元素的高度等信息
		// 		console.log('data.height',data.height)  // 获取元素宽度
		// }).exec(function(res){
		// 	// 注意：exec方法必须执行，即便什么也不做，否则不会获取到任何数据
		// })
        this.clientWidth = 70
        this.clientHeight = 70
        this.left = this.clientWidth - this.itemWidth - this.gapWidth
        this.top = this.clientHeight - this.itemWidth - this.gapWidth - 40
    },
    mounted() {
		// let floatBtnTop = ''
		// let floatBtnLeft = ''
		// let info = uni.createSelectorQuery().in(this).select(".aaa");
		// info.boundingClientRect(function(data) { 
		// 	//	data - 包含元素的高度等信息
		// 		console.log('data.height',data.height)  // 获取元素宽度
		// }).exec(function(res){
		// 	// 注意：exec方法必须执行，即便什么也不做，否则不会获取到任何数据
		// })
		// // 计算按钮的left和top
		
		// console.log('floatBtnTop,floatBtnLeft',floatBtnTop,floatBtnLeft)
		// localStorage.setItem('floatBtnTop',floatBtnTop)
		// localStorage.setItem('floatBtnLeft',floatBtnLeft)
    },
    methods: {
        onBtnClicked(){
			console.log('onBtnClicked')
            this.$emit("onFloatBtnClicked")
        },
        handleScrollStart() {
            this.timer && clearTimeout(this.timer)
            this.timer = setTimeout(() => {
                this.handleScrollEnd()
            }, 300)
            this.currentTop = document.documentElement.scrollTop || document.body.scrollTop
            if(this.left > this.clientWidth / 2) {
                this.left = this.clientWidth - this.itemWidth / 2
            } else {
                this.left = -this.itemWidth / 2
            }
        },
        handleScrollEnd(){
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            if(scrollTop === this.currentTop) {
                if(this.left > this.clientWidth/2) {
                    this.left = this.clientWidth - this.itemWidth - this.gapWidth
                } else {
                    this.left = this.gapWidth
                }
                clearTimeout(this.timer)
            }
        },
		touchStart() {
			// const floatButton = this.$refs.floatButton
			this.floatButton.style.transition = 'none'
			// this.$emit('forbidScroll')
			document.body.style.overflow = 'hidden'
		},
		touchmove(e) {
			if (e.targetTouches.length === 1) {         // 一根手指
			    let touch = e.targetTouches[0]
			    this.left = touch.clientX - (this.itemWidth/2)
			    this.top = touch.clientY - (this.itemWidth/2)
			}
		},
		touchend() {
			document.body.style.overflow = 'auto'
			this.floatButton.style.transition = 'all 0.3s'
			if(this.left > document.documentElement.clientWidth / 2) {
			    this.left = document.documentElement.clientWidth - this.itemWidth- this.gapWidth
			}else{
			    this.left = 0 + this.gapWidth
			}
			if(this.top>(document.documentElement.clientHeight-this.itemWidth)) {
				this.top = document.documentElement.clientHeight-this.itemWidth- this.gapWidth
			}
			if(this.top<this.itemWidth) {
				this.top = 0 + this.gapWidth
			}
		},
    },
}
</script>

